package forms;

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

public class MyForm extends JDesktopPane {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void paint(Graphics g) {

		super.paint(g);
		Dimension d = preferredSizeOfAllFrames();
		this.setPreferredSize(d);
		this.revalidate();
		// super.paintComponent(g);
//		ImageIcon backgroundImage = new ImageIcon("img//mainback.jpg");
//		if (backgroundImage != null) {
//			backgroundImage.setImage(
//					backgroundImage.getImage().getScaledInstance(this.getWidth(), this.getHeight(), Image.SCALE_FAST));
//			backgroundImage.paintIcon(this, g, 0, 0);
//		}
	}

	public Dimension preferredSizeOfAllFrames() {
		JInternalFrame[] array = getAllFrames();
		int maxX = 0;
		int maxY = 0;
		for (int i = 0; i < array.length; i++) {
			if (array[i].isVisible()) {
				int cx;
				cx = array[i].getX();
				int x = cx + array[i].getWidth();
				if (x > maxX)
					maxX = x;

				int cy;
				cy = array[i].getY();
				int y = cy + array[i].getHeight();
				if (y > maxY)
					maxY = y;
			}
		}
		return new Dimension(0, 0);
	}

	public void showForm(JInternalFrame frame) {
		if (getSelectedFrame() != null) {
			getSelectedFrame().dispose();
		}

		this.add(frame);
		frame.setVisible(true);
	
	}
 
}
